import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/sale_bid_project_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_materialcheck_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_bid_project.dart';
import 'package:flutter_admin/models/odoo/stock_materialcheck.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/form/index.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/pages/odoo/sale_bid_project/sale_bid_project_form_select.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class StockMaterialcheckEdit extends StatefulWidget {
  StockMaterialcheckEdit({this.stockMaterialcheck});

  final StockMaterialcheck? stockMaterialcheck;

  @override
  _StockMaterialcheckEditState createState() => _StockMaterialcheckEditState();
}

class _StockMaterialcheckEditState extends State<StockMaterialcheckEdit> {
  late StockMaterialcheck stockMaterialcheck;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  DateFormat format = DateFormat('yyyy-MM-dd');

  List<SelectOptionVO> locationList = [];
  UserInfo userInfo = StoreUtil.getCurrentUserInfo();

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.stockMaterialcheck == null;
    this.stockMaterialcheck = widget.stockMaterialcheck ?? StockMaterialcheck();
    _loadSelectVO();
  }

  _loadSelectVO() async {

    if(isDisplayDesktop(context)){
      // if (userInfo.major != null) {
      locationList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'father_locaiton',
          val: null
      );
      // }
    }else{
      ResponseBodyApi res = await StockMoveLineOdooApi.getLocation(userInfo.userId);
      if (res.success!){
        for(var i = 0; i < res.data.length; i++){
          locationList.add(SelectOptionVO(value: res.data[i]["locaiton_id"].toString(),label: res.data[i]["locaiton_name"]));
        }
        if(res.data.length == 1){
          stockMaterialcheck.warehouseId = res.data[0]["location_id"].toString();
        }
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          isAdd!?CrySelect(
            label: '盘点库房',
            dataList: locationList,
            value: stockMaterialcheck.warehouseId,
            onSaved: (v) {
              stockMaterialcheck.warehouseId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ):CryInput(
            label: '盘点库房',
            enable: false,
            value: stockMaterialcheck.warehouseName,
          ),
          CryInput(
            label: '盘点计划',
            value: stockMaterialcheck.name,
            onSaved: (v) {
              stockMaterialcheck.name = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '备注',
            value: stockMaterialcheck.remark,
            onSaved: (v) {
              stockMaterialcheck.remark = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            StockMaterialcheckOdooApi.saveOrUpdate(stockMaterialcheck.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd!
            ? S.of(context).add
            : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
